import { TRADE_ORDER_BOARD_ACTION } from '../reducers/tradeOrderBoardReducer'

import { intl } from '../../intl/intl'

import { order as orderRequest } from '../../api/orderApi'
import { formatValue, getContractPriceDecimalNumber } from '../../utils/dataUtils'

export const ORDER_OFFSET_TYPE_OPEN = 'open_position'
export const ORDER_OFFSET_TYPE_CLOSE = 'close_position'

/**
 * 设置开平仓类型
 * @param offsetType
 * @returns {{data: {currentOrderOffsetType: *}, type: string}}
 */
export function setOrderOffsetType (offsetType) {
  return {
    type: TRADE_ORDER_BOARD_ACTION,
    data: {
      currentOrderOffsetType: offsetType,
      price: '',
      volume: '',
      errorTipText: ''
    }
  }
}

/**
 * 价格输入框变化事件
 * @param e
 */
export function priceInputChange (e) {
  return (dispatch, getState) => {
    const value = e.target.value
    const decimal = getContractPriceDecimalNumber(getState().trade.market.get('currentContract'))
    const reg = new RegExp('^\\d+\\.?\\d{0,' + decimal + '}$')
    if (value === '' || reg.test(value)) {
      dispatch({
        type: TRADE_ORDER_BOARD_ACTION,
        data: {
          price: value
        }
      })
    }
  }
}

/**
 * 输入框获取焦点事件
 * @returns {{data: {errorTipText: string}, type: string}}
 */
export function inputFocus () {
  return {
    type: TRADE_ORDER_BOARD_ACTION,
    data: {
      errorTipText: ''
    }
  }
}

/**
 * 数量输入框变化事件
 * @param e
 */
export function volumeInputChange (e) {
  return dispatch => {
    const value = e.target.value
    if (value === '' || /^\d+$/.test(value)) {
      dispatch({
        type: TRADE_ORDER_BOARD_ACTION,
        data: {
          volume: value
        }
      })
    }
  }
}

/**
 * 设置买卖盘价格
 */
export function setOrderBidAskPrice (price) {
  return dispatch => {
    dispatch({
      type: TRADE_ORDER_BOARD_ACTION,
      data: {
        price: price || ''
      }
    })
  }
}

/**
 * 下单
 * @param direction
 * @param success
 */
export function order (direction, success) {
  return (dispatch, getState) => {
    const currentContract = getState().trade.market.get('currentContract')
    if (!currentContract) {
      dispatch({
        type: TRADE_ORDER_BOARD_ACTION,
        data: {
          errorTipText: intl('contract_not_found')
        }
      })
      return
    }
    const state = getState().trade.orderBoard
    const price = state.get('price')
    if (price === '') {
      dispatch({
        type: TRADE_ORDER_BOARD_ACTION,
        data: {
          errorTipText: intl('please_input_price')
        }
      })
      return
    }
    if ((+price) === 0) {
      dispatch({
        type: TRADE_ORDER_BOARD_ACTION,
        data: {
          errorTipText: intl('price_not_be_zero')
        }
      })
      return
    }
    const volume = state.get('volume')
    if (!volume) {
      dispatch({
        type: TRADE_ORDER_BOARD_ACTION,
        data: {
          errorTipText: intl('please_input_volume')
        }
      })
      return
    }
    const currentOrderOffsetType = state.get('currentOrderOffsetType')
    const offsetType = currentOrderOffsetType === ORDER_OFFSET_TYPE_OPEN ? '0' : '1'
    const params = {
      MemberID: '1001', UserID: '100101', TradeUnitID: '10010001',
      InstrumentID: formatValue(currentContract, 'InstrumentID'),
      ExchangeID: formatValue(currentContract, 'ExchangeID'),
      LimitPrice: +price, Volume: +volume, Direction: direction,
      OffsetFlag: offsetType
    }
    orderRequest(params).then(
      result => {
        if (success) {
          success()
        }
      }
    ).then(_ => {
    })
  }
}
